import http from "./http";
import _ from 'lodash';
import {password} from '../utils/encryption'

function getFactorys(name=undefined){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  return http.get("/api/factory/query/names",{
    params:{
      name:name
    }
  }).then(res =>{
    if(res.code){
      return Promise.resolve(res.data);
    }
  })
}

function login(data){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  const params=_.cloneDeep(data);
  params.password=password(params.password).toString().toUpperCase();
  return http.post("/api/factory/admin/login",params).then(res =>{
    if(res.code){
      return Promise.resolve(res.data);
    }
  })
}

function addEnterRecord(data){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  return http.post("/api/visitor/register/enter",data).then(res =>{
    if(res.code){
      return Promise.resolve(res.data);
    }
  })
}

function getQRCodes(data){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  return http.post("/api/qr_code/page",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function fileUrl(code){
  return `${process.env.VUE_APP_API_BASE}/api/file/download?key=${code}`;
}

function downloadFile(code){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  return http.get("/api/file/download",{
    params:{
      key:code
    },
    responseType:"blob"
  })
}

function getFormmain(limit){
  http.defaults.baseURL=process.env.VUE_APP_API_BASE;
  return http.get("/api/formmain/show",{
    params:{
      limit
    }
  })
}

export {
  getFactorys, login,
  addEnterRecord,
  getQRCodes, fileUrl, downloadFile, getFormmain
}